<%--
  Created by IntelliJ IDEA.
  User: thankyouzero
  Date: 2018/5/2
  Time: 18:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="java.util.*,java.sql.*,javax.servlet.*,functest.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>具体购票页面</title>
    <link href="css/style.css" type="text/css" rel="stylesheet" />
    <link href="layui/css/layui.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="layui/layui.all.js"></script>
    <script type="text/javascript" src="js/jquery-3-2-1.js"></script>
</head>
<body background="img/background.png">
<div class="main">
    <div class="head" >
        <div class="head-text">
            <li class="title">购票页面</li>
        </div>
        <a href="index.jsp"><img class="logo" src="img/logo.png" ></a>
    </div>

    <div class="content">
        <%
            //连接数据库
            String driver="com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
            String user="hbpwuser";
            String pass="LFFLXY";
            Class.forName(driver);                   //加载jdbc驱动
            Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);

            Statement statement = (Statement) connection.createStatement();  //连接实例
        %>
        <%
            request.setCharacterEncoding("utf-8");//解决编码问题
            String HBH= request.getParameter("HBH");
            String JC_START =request.getParameter("stjc");
            String JC_END = request.getParameter("enjc");
            String month= request.getParameter("month");
            String day= request.getParameter("day");
            String name = request.getParameter("name");
            String userid = request.getParameter("userid");
            String tkid = request.getParameter("tkid");
            String date="2018-";
            if(Integer.valueOf(month).intValue()<10)
                date=date+"0";
            date=date+month+"-";
            if(Integer.valueOf(day).intValue()<10)
                date=date+"0";
            date=date+day;

            //查询始终机场代码
            String sql1 ="SELECT jc1.JC_DM AS 出发机场代码,jc2.JC_DM  AS 到达机场代码"+
                    " FROM jc jc1,jc jc2"+
                    " WHERE jc1.JC_MC='"+JC_START+"'"+
                    " AND jc2.JC_MC='"+JC_END+"';";
            ResultSet Rst1 = statement.executeQuery(sql1);
            String 出发机场代码=null;
            String 到达机场代码=null;
            while(Rst1.next()){
                出发机场代码= Rst1.getString("出发机场代码");
                到达机场代码= Rst1.getString("到达机场代码");
            }
            Rst1.close();
            //查询涉及到的起始航段顺序号
            String sql2 ="SELECT  s.HD_SXH AS 起飞机场航段号,e.HD_SXH  AS 到达机场航段号"+
                    " FROM hd s,hd e"+
                    " WHERE"+
                    " s.JC_START='"+出发机场代码+"'"+
                    " AND e.JC_END='"+到达机场代码+"' AND s.HB_HBH='"+HBH+"' AND e.HB_HBH=s.HB_HBH;";
            ResultSet Rst2 = statement.executeQuery(sql2);
            String 起飞机场航段号=null;
            String 到达机场航段号=null;
            while(Rst2.next()){
                起飞机场航段号= Rst2.getString("起飞机场航段号");
                到达机场航段号= Rst2.getString("到达机场航段号");
            }
            Rst2.close();
        %>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>填写您的信息</legend>
        </fieldset>
        <form method="POST" class="layui-form" name="airport" action="handletk.jsp<%
            if (name != null){
                out.println("?tkid=" + tkid);
            }
        %>">
            <div class="layui-form-item">
                <label class="layui-form-label">航班号</label>
                <div class="layui-input-block">
                    <input type="text" name="HBH" readonly class="layui-input" value="<%=HBH%>"></td>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">出发机场</label>
                <div class="layui-input-block">
                    <input type="text" name="JC_START" readonly class="layui-input" value="<%=JC_START%>">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">到达机场</label>
                <div class="layui-input-block">
                    <input type="text" name="JC_END" readonly class="layui-input" value="<%=JC_END%>">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">出发日期</label>
                <div class="layui-input-block">
                    <input type="text" name="date" readonly class="layui-input" value="<%=date%>">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">舱位</label>
                <div class="layui-input-block">
                    <%
                        //处理仓位,正则表达式匹配
                        String 仓位总数类型=null;
                        String 仓位价格类型=null;
                        仓位总数类型 = "hb.HB_TDCS";
                        仓位价格类型="HD_TDCJG";
                        //获取头等舱剩余座位及价格
                        String sql3="SELECT "+仓位总数类型+"-count(SPXX_QFHDH)  AS 当天旅程剩余座位 FROM spxx,hb" +
                                " WHERE SPXX_RQ='"+date+"' AND SPXX_CW='头等舱' AND spxx.HB_HBH='"+HBH+"' AND"+
                                " SPXX_QFHDH<="+到达机场航段号+" AND SPXX_DDHDH>="+起飞机场航段号+" AND hb.HB_HBH='"+HBH+"';";
                        ResultSet Rst3 = statement.executeQuery(sql3);
                        String 当天旅程剩余座位=null;
                        while(Rst3.next()){
                            当天旅程剩余座位= Rst3.getString("当天旅程剩余座位");//座位问题
                        }
                        String 价格=null;
                        //计算价格
                        String sql4 =" SELECT sum("+仓位价格类型+") AS 价格 FROM hd"+
                                " WHERE HD_SXH>="+起飞机场航段号+" AND HD_SXH<="+到达机场航段号+
                                " AND HB_HBH='"+HBH+"';";
                        ResultSet Rst4 = statement.executeQuery(sql4);
                        while(Rst4.next()){
                            价格= Rst4.getString("价格");
                        }
                        Rst4.close();
                        Rst3.close();
                    %>
                    <input type="radio" name="Cangwei" value="头等舱" title="头等舱<br/>
                    剩余座位：<%=当天旅程剩余座位%><br/>
                    价格：<%=价格%>" <%if( Integer.valueOf(当天旅程剩余座位).intValue()<1) out.println("disabled=\"\"");%>>
                    <%
                        //获取经济舱剩余座位及价格
                        仓位总数类型 = "hb.HB_JJCS";
                        仓位价格类型="HD_JJCJG";
                        sql3="SELECT "+仓位总数类型+"-count(SPXX_QFHDH)  AS 当天旅程剩余座位 FROM spxx,hb" +
                                " WHERE SPXX_RQ='"+date+"' AND SPXX_CW='经济舱' AND spxx.HB_HBH='"+HBH+"' AND"+
                                " SPXX_QFHDH<="+到达机场航段号+" AND SPXX_DDHDH>="+起飞机场航段号+" AND hb.HB_HBH='"+HBH+"';";
                        Rst3 = statement.executeQuery(sql3);
                        当天旅程剩余座位=null;
                        while(Rst3.next()){
                            当天旅程剩余座位= Rst3.getString("当天旅程剩余座位");//座位问题
                        }
                        价格=null;
                        //计算价格
                        sql4 =" SELECT sum("+仓位价格类型+") AS 价格 FROM hd"+
                                " WHERE HD_SXH>="+起飞机场航段号+" AND HD_SXH<="+到达机场航段号+
                                " AND HB_HBH='"+HBH+"';";
                        Rst4 = statement.executeQuery(sql4);
                        while(Rst4.next()){
                            价格= Rst4.getString("价格");
                        }
                        Rst3.close();
                        Rst4.close();
                    %><input type="radio" name="Cangwei" value="经济舱" title="经济舱<br/>
                        剩余座位：<%=当天旅程剩余座位%><br/>
                        价格：<%=价格%>" <%if( Integer.valueOf(当天旅程剩余座位).intValue()<1) out.println("disabled=\"\"");%>>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <%
                        if (name == null){
                         %>
                    <input class="layui-input" maxlength="10" lay-verify="username" required="required" placeholder="请输入姓名" type="text" name="name"/>
                    <%
                        }
                        else {
                    %>
                    <input class="layui-input" maxlength="10" type="text" readonly name="name" value="<%=name%>"/>
                    <%
                        }
                    %>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">身份证号</label>
                <div class="layui-input-block">
                    <%
                        if (userid == null){
                        %>
                    <input required="required" type="text" name="identity" lay-verify="identity" placeholder="请输入18位身份证号" class="layui-input">
                    <%
                        }
                        else {
                            %>
                    <input type="text" name="identity" readonly class="layui-input" value="<%=userid%>">
                    <%
                        }
                    %>
                </div>
            </div>
            <div class="layui-form-item" style="text-align: center;">
                <button lay-submit="" id="submit" class="layui-btn">购买</button>
            </div>
        </form>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    $(function(){
        $('#submit').click(function(){
            if($("input[name='Cangwei']:checked").length == 0){
                alert("最少选择一个舱位!");
                return false;
            }
            else $('form').submit();
        });
    })
</script>
<script type="text/javascript">
    layui.use(['form','layer'],function (){
        var form = layui.form
            ,layer = layui.layer;
        form.render();
        form.verify({
            username: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[\u4e00-\u9fa5{2,8}]+$").test(value)){
                    return '外国人和恶意测试者不可购买';
                }
            }

            //我们既支持上述函数式的方式，也支持下述数组的形式
            //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
            ,identity: [
                /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
                ,'身份证有误'
            ]
        });
        form.render();
    })
</script>
